Page({
    data: {
        selectedItems: [],
        totalAmount: 0,
        paymentMethods: [
            {
                id: 'wechat',
                name: '微信支付',
                icon: '💚',
                description: '使用微信支付安全快捷',
                available: true
            },
            {
                id: 'alipay',
                name: '支付宝支付',
                icon: '🔵',
                description: '使用支付宝支付便捷安全',
                available: true
            }
        ],
        selectedMethod: 'wechat'
    },

    onLoad() {
        // 获取选中的缴费项目
        const selectedItems = wx.getStorageSync('selectedPaymentItems') || [];
        const totalAmount = selectedItems.reduce((sum, item) => sum + item.amount, 0);

        this.setData({
            selectedItems,
            totalAmount
        });
    },

    // 选择支付方式
    selectPaymentMethod(e) {
        const method = e.currentTarget.dataset.method;
        this.setData({
            selectedMethod: method
        });
    },

    // 确认支付
    confirmPayment() {
        const { selectedMethod, selectedItems, totalAmount } = this.data;

        if (!selectedMethod) {
            wx.showToast({
                title: '请选择支付方式',
                icon: 'none'
            });
            return;
        }

        // 模拟支付流程
        wx.showLoading({
            title: '处理中...'
        });

        setTimeout(() => {
            wx.hideLoading();

            // 模拟支付成功
            this.handlePaymentSuccess();
        }, 2000);
    },

    // 处理支付成功
    handlePaymentSuccess() {
        const { selectedItems, totalAmount, selectedMethod } = this.data;

        // 生成支付记录
        const paymentRecord = {
            id: Date.now(),
            items: selectedItems,
            totalAmount,
            paymentMethod: selectedMethod,
            status: 'paid',
            paymentTime: new Date().toLocaleString('zh-CN'),
            transactionId: 'TXN' + Date.now()
        };

        // 保存支付记录
        let paymentRecords = wx.getStorageSync('paymentRecords') || [];
        paymentRecords.unshift(paymentRecord);
        wx.setStorageSync('paymentRecords', paymentRecords);

        // 更新缴费状态
        this.updatePaymentStatus();

        // 清除选中的项目
        wx.removeStorageSync('selectedPaymentItems');

        // 显示支付成功
        wx.showModal({
            title: '支付成功',
            content: `已成功支付￥${totalAmount.toFixed(2)}`,
            showCancel: false,
            success: () => {
                wx.navigateBack();
            }
        });
    },

    // 更新缴费状态
    updatePaymentStatus() {
        // 这里应该调用API更新服务器端的缴费状态
        // 暂时使用本地存储模拟
        const { selectedItems } = this.data;
        selectedItems.forEach(item => {
            item.status = 'paid';
        });
    },

    // 查看订单详情
    viewOrderDetails() {
        const { selectedItems, totalAmount } = this.data;
        let content = '缴费详情：\n';
        selectedItems.forEach(item => {
            content += `${item.title}：￥${item.amount.toFixed(2)}\n`;
        });
        content += `\n总计：￥${totalAmount.toFixed(2)}`;

        wx.showModal({
            title: '订单详情',
            content,
            showCancel: false
        });
    }
}); 